<?php /** @var $this \Action\Page\Edit */

use Action\Page\Save;
use Model\Page; ?>

<?php echo $this->outputHeader() ?>
<?php 
    $paramsTypeCreate = null;
    $_id = 0;
    $_idstr = '';
    $genid = function() use(&$_id, &$_idstr) {
        $_id ++;
        $_idstr = "genid_{$_id}";
        return $_idstr;
    };
    
    $getid = function() use(&$_id, &$_idstr)  {
        return $_idstr;
    };
    
    $tpls = [];
    $richedits = [];
    $codeEditors = [];
?>

<link rel="stylesheet" href="/static/css/page/edit.css" />
<link rel="stylesheet" href="/static/ueditor/third-party/codemirror/codemirror.css" />
<script type="text/javascript" src="/static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="/static/ueditor/ueditor.all.js"></script>
<script type="text/javascript" src="/static/ueditor/third-party/codemirror/codemirror.js"></script>

<!-- 选择模板 -->
<?php if(!$this->isEdit()): ?>
    <?php if($this->templateSelect): ?>
        <div class="form-body">
            <form action="">
                <div class="line">
                    <div class="label">选择模板</div>
                    <div class="value">
                        <select name="template">
                            <?php foreach ($this->getPageTemplates() as $tem): ?>
                                <option value="<?php echo $tem['key'] ?>"><?php echo $tem['name'] ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <div class="line">
                    <div class="value submit-btn-bar">
                        <input type="hidden" name="a" value="<?php echo $_REQUEST['a'] ?>">
                        <button class="button" type="submit">下一步</button>
                    </div>
                </div>
            </form>
        </div>
    <?php endif; ?>
<?php endif; ?>

<!-- 编辑 -->
<?php if(!$this->templateSelect): ?>
<div class="form-body">
    <div style="display: none;" key="$this->pageParams">
        <xmp>
            <?php print_r($this->pageParams) ?>
        </xmp>
    </div>
    <div style="display: none;" key="$this->pageData">
        <xmp>
            <?php print_r($this->pageData) ?>
        </xmp>
    </div>
    <form id="edit-page-form" method="post" action="/?a=<?php echo Save::getActionStr() ?>">
        <input type="hidden" name="template" value="<?php echo $this->pageData['template'] ?>">
        <input type="hidden" name="id" value="<?php echo $this->pageData['id'] ?>">
    
        <div class="line">
            <div class="label">模板</div>
            <div class="value"><?php echo $this->pageParams['name'] ?></div>
        </div>
        <div class="line">
            <div class="label">页面名称</div>
            <div class="value"><input type="text" name="name" value="<?php echo $this->pageData['name'] ?>"/></div>
        </div>
        <div class="line">
            <div class="label">页面说明</div>
            <div class="value"><input type="text" name="desc" value="<?php echo $this->pageData['desc'] ?>" /></div>
        </div>
        <div class="line">
            <div class="label">开启</div>
            <div class="value">
                <label><input type="radio" value="1" name="enabled" <?php echo ['', 'checked'][intval($this->pageData['enabled'])] ?> />启用</label>&nbsp;
                <label><input type="radio" value="" name="enabled" <?php echo ['checked',''][intval($this->pageData['enabled'])] ?> />禁用</label>
            </div>
        </div>
        <div class="line divider"></div>
        <div class="line">
            <div class="label">页面标题</div>
            <div class="value code-editor"><input type="text" name="data[title]" value="<?php echo $this->pageData['data']['title'] ?>" /></div>
        </div>
        <div class="line">
            <div class="label">头部代码</div>
            <div class="value code-editor">
                <?php $codeEditors []= 'head_html_textarea' ?>
                <textarea type="text" id="<?php echo end($codeEditors) ?>" name="data[head_html]" ><?php echo $this->pageData['data']['head_html'] ?></textarea>
            </div>
        </div>
        <div class="line">
            <div class="label">底部代码</div>
            <div class="value code-editor">
                <?php $codeEditors []= 'bottom_html_textarea' ?>
                <textarea type="text" id="<?php echo end($codeEditors) ?>" name="data[bottom_html]" ><?php echo $this->pageData['data']['bottom_html'] ?></textarea>
            </div>
        </div>
        <div class="line divider"></div>
        <?php $contentStack = [] ?>
        <?php 
            $paramsTypeCreate = function($paramKey, $d, $parentFormFieldName='', $keyValues='', $isList=false, $level=0)
                use(&$paramsTypeCreate, $genid, $getid, &$tpls, &$richedits) {
                ob_start();
                include __DIR__ . '/edit.form.phtml';
                $str = ob_get_contents();
                ob_end_clean();
                return $str;
            }
        ?>
        <?php 
        foreach ((array)$this->pageParams['fields'] as $key => $conf) {
            echo $paramsTypeCreate($key, $conf, 'data[fields]', $this->pageData['data']['fields'][$key]);
        } 
        ?>
    
            <?php foreach ($tpls as $tpl): ?>
                <?php echo $tpl ?>
            <?php endforeach; ?>
    </form>
<!--    -->
<!--    <xmp>-->
<!--        --><?php //print_r($this->pageParams); ?>
<!--        --><?php //print_r($this->pageData); ?>
<!--    </xmp>-->
<!--   -->
</div>
<div class="submit-btn-bar">
    <button type="submit" onclick="submit_form()">保存</button>
</div>

<?php endif; ?>

<script>
    var richeditIncrementId = 1;
    
    function add_list_item(elm) {
        let dataid = elm.getAttribute('dataid');
        let script = document.getElementById('tpl_' + dataid);
        let container = elm.parentNode.querySelector(':scope > .list-child');
        let html = script.innerHTML;
        
        let increment = elm.getAttribute('increment');
        increment = parseInt(increment);

        html = html.replace(new RegExp('\\{seq_'+dataid+'}', 'g'), increment);
        increment ++;
        elm.setAttribute('increment', increment);
            
        let div = document.createElement('div')
        div.classList.add('append-row')
        div.setAttribute('class', 'list-item-wrap');
        div.innerHTML = html;
        let node = div.children[0];
        container.appendChild(node);
        
        node.querySelectorAll('textarea.richedit-noinit').forEach(row => {
            init_richedit(row);
        });
    }
    
    function remove_list_item(elm) {
        let parent = elm.parentNode.parentNode;
        parent.parentNode.removeChild(parent);
    }
    
    function mouseover_remove_item(elm) {
        let parent = elm.parentNode.parentNode;
        parent.classList.add('which');
    }
    
    function mouseout_remove_item(elm) {
        let parent = elm.parentNode.parentNode;
        parent.classList.remove('which');
    }
    
    function submit_form() {
        document.getElementById('edit-page-form').submit();
    }
    
    function init_richedit(elm) {
        if(!elm.id) {
            elm.id = 'richedit_auto_gen_' + richeditIncrementId ++;
        } 
        
        UE.getEditor(elm.id);
        elm.classList.remove('richedit-noinit');
    }
</script>

<script>
    <?php if(!empty($codeEditors)): ?>
    <?php foreach($codeEditors as $editId): ?>
    CodeMirror.fromTextArea(document.getElementById('<?php echo $editId ?>'), {
        lineNumbers: true,
        mode:'htmlmixed'
    });
    <?php endforeach; ?>
    <?php endif; ?>

    document.querySelectorAll('textarea.richedit-noinit').forEach(row => {
        init_richedit(row);
    });
</script>

<?php echo $this->outputFooter() ?>
